package com.ijava.base;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;

import javax.sql.DataSource;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Spring1 {
	Logger logger = LoggerFactory.getLogger(Spring1.class);

	@Test
	public void test() {
		try {

			ApplicationContext context = new ClassPathXmlApplicationContext(
					new String[] { "applicationContext-base.xml" });

			//SPRING JDBC
			DataSource dataSource = context
					.getBean(ComboPooledDataSource.class);

			JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
			Date now = jdbcTemplate.queryForObject("SELECT NOW()",
					new RowMapper<Date>() {
						public Date mapRow(ResultSet rs, int arg1)
								throws SQLException {
							return rs.getDate(1);
						}
					});
			
			Assert.assertEquals(now.toString(),
					new Date(System.currentTimeMillis()).toString());
			
			//I18N
			ResourceBundleMessageSource ms = context.getBean(ResourceBundleMessageSource.class);
			String loginId = ms.getMessage("loginId", null, Locale.getDefault());
			Assert.assertEquals("登陆名称",
					loginId);
			
		} catch (BeansException e) {
			logger.error("init error", e);
		} catch (Exception e) {
			logger.error("error", e);
		}
	}

}
